﻿#include "gateway.h"
#include "src/bend/man/mancloud.h"
#include "src/config/ErrorCode.h"
#include "src/config/apis.h"
#include "src/config/baseexception.h"
#include "src/config/loggerproxy.h"
#include "src/middle/manglobal.h"
#include "src/middle/signals/mansignals.h"
#include <QDebug>
#include <QtConcurrent>
GateWay::GateWay(QObject* parent)
    : QObject(parent)
{}

GateWay::~GateWay()
{
    qDebug() << "delete Gateway";
}

void GateWay::send(int api, const QJsonValue& value)
{
    QtConcurrent::run([=]() {
        try {
            this->dispatch(api, value);
        }
        catch (BaseException e) {

            mError(e.msg());
            emit MG->mSignal->error(api, e.msg());
        }
        catch (...) {
            BaseException e = BaseException(EC_100000, QString::fromLocal8Bit("未知错误"));
            mError(e.msg());
            emit MG->mSignal->error(api, e.msg());
        }
    });
}

void GateWay::dispatch(int api, const QJsonValue& value)
{
    switch (api) {
    case API::LOGIN::NORMAL: apiLogin(value); break;
    default: break;
    }
}

void GateWay::apiLogin(const QJsonValue& value)
{
    QString secretId  = value["secretId"].toString();
    QString secretKey = value["secretKey"].toString();
    MG->mCloud->login(secretId, secretKey);
    emit MG->mSignal->loginSuccess();
}
